﻿using System.Windows;
using DoVuiKinhThanh.Domain;
using System.Windows.Documents;

namespace DoVuiKinhThanh
{

    /// <summary>
    /// Interaction logic for QuizEditing.xaml
    /// </summary>
    public partial class QuizEditing : Window
    {
        #region IConstructors

        /// <summary>
        /// Initializes a new instance of the <see cref="QuizEditing"/> class.
        /// </summary>
        public QuizEditing()
            : this(null)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="QuizEditing"/> class.
        /// </summary>
        /// <param name="editingQuiz">The editing quiz.</param>
        public QuizEditing(Quiz editingQuiz)
        {
            InitializeComponent();
            EditingQuiz = editingQuiz ?? new Quiz();            
        }

        #endregion

        #region IMethods

        private void OnBtnSaveQuizClicked(object sender, RoutedEventArgs e)
        {
            if (EditingQuiz != null)
            {
                var textRange = new TextRange(m_ctrlQuiz.QuizRichTextBox.Document.ContentStart, m_ctrlQuiz.QuizRichTextBox.Document.ContentEnd);
                EditingQuiz.ContentWithoutFormat = textRange.Text;
            }
            DialogResult = true;
            Close();
        }

        private void OnBtnCancelClicked(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            Close();
        }

        private void OnWindowLoaded(object sender, RoutedEventArgs e)
        {
            m_ctrlQuiz.BindQuizData(EditingQuiz);
        }

        #endregion

        #region IProperties

        /// <summary>
        /// Gets or sets the editing quiz.
        /// </summary>
        /// <value>
        /// The editing quiz.
        /// </value>
        public Quiz EditingQuiz
        {
            get;
            set;
        }

        #endregion
    }
}